<?php
declare (strict_types = 1);

namespace app\common\command;

use think\console\Command as TopCommand;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use app\common\model\Payment;
use app\admin\model\SubscribeOrder;
use app\admin\model\SubscribeRecord;
use think\facade\Db;
use ball\JinyeLightIot;
class Order extends TopCommand
{
    protected function configure()
    {
        // 指令配置
        $this->setName('order')
            ->setDescription('the order command');
    }

    protected function execute(Input $input, Output $output)
    {
        // 指令输出
        $output->writeln('order start');
        if(date('i')=='58'){
            $this->orderLight();
        }
        if(date('Hi')=='2330'){
            $this->userVerify();
        }
        $this->orderVerify();
        $output->writeln('order end');
    }
    protected function userVerify(){
        $ids = Db::name('User')->where('group_id',3)->where('auth_end','<',time())->update(['group_id'=>'2']);
    }
    protected function orderVerify(){
        $orderlist = SubscribeOrder::where('pay_status',0)->select();
        if(!empty($orderlist)){
            foreach ($orderlist as $key => $order) {
                $rlt = Payment::verify($order['out_trade_no']);
                if($rlt === false){
                    if(time()-$order['update_time'] >300){
                        if($order['out_trade_type'] == '6'){
                            SubscribeRecord::where('order_id',$order['id'])->delete();
                        }
                        $order->delete();
                    }
                }
            }
        }
    }

    protected function orderLight(){
        $record =SubscribeRecord::where('state','in','2,3,4')->where('status',1)->where('year',date('Y'))->where('month',date('m'))
        ->where('days',date('d'))->where('hour',date('h',strtotime('+1 hour')))->column('stadium_id');
        if(!empty($record)){
            $stadium = Db::name('Stadium')->where('id','in',$record)->select();
            $light = [];
            foreach ($stadium as $key => $value) {
                $light[]=$value['before_id'];
                $light[]=$value['after_id'];
            }
            $lightCtr = Db::name('LightingControl')->where('id','in',$light)->select();
            $JLIot = new JinyeLightIot();
            foreach ($lightCtr as $key => $value) {
                $JLIot->channelSwitch($value['channel'],$value['rtu_addr'],'ON');
            }
        }
    }
}
